package com.azkj.album.utils;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;

import java.util.Calendar;

public class SelectDateUtil {


    public static void showDatePickDialog(Context context, DatePickerDialog.OnDateSetListener listener, String curDate) {
        Calendar calendar = Calendar.getInstance();
        int year = 0, month = 0, day = 0;
        try {
            year = Integer.parseInt(curDate.substring(0, curDate.indexOf("-")));
            month = Integer.parseInt(curDate.substring(curDate.indexOf("-") + 1, curDate.lastIndexOf("-"))) - 1;
            day = Integer.parseInt(curDate.substring(curDate.lastIndexOf("-") + 1, curDate.length()));
        } catch (Exception e) {
            e.printStackTrace();
            year = calendar.get(Calendar.YEAR);
            month = calendar.get(Calendar.MONTH);
            day = calendar.get(Calendar.DAY_OF_MONTH);

        }

        DatePickerDialog datePickerDialog = new DatePickerDialog(context, DatePickerDialog.THEME_HOLO_LIGHT, listener, year, month, day);
        datePickerDialog.show();
    }

    public static void showTimePickDialog(Context context, TimePickerDialog.OnTimeSetListener listener, String curDate) {
        TimePickerDialog dialog;
        Calendar calendar = Calendar.getInstance();
        String[] split = curDate.split(":");


        if (split.length > 1) {
            dialog = new TimePickerDialog(context, TimePickerDialog.THEME_HOLO_LIGHT, listener, Integer.parseInt(split[0]), Integer.parseInt(split[1]),
                    true);
        } else {
            //true表示使用二十四小时制
            dialog = new TimePickerDialog(context, TimePickerDialog.THEME_HOLO_LIGHT, listener,
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    true);
        }

        dialog.show();
    }

}